// 不做mysql , json
const Koa = require('koa')
const router = require('koa-router')();
const app = new Koa()
// const MainData = require('./Data/mainData/MianData.json')
// const ServerData = require('./Data/serverData/ServerData.json')
const cors = require('koa2-cors')
const Mock = require('mockjs')
const Random = Mock.Random

app.use(cors({
    origin: function(ctx) { //设置允许来自指定域名请求
        // if (ctx.url === '/test') {
        return '*'; // 允许来自所有域名请求
        // }
    },
    maxAge: 5, //指定本次预检请求的有效期，单位为秒。
    credentials: true, //是否允许发送Cookie
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
}))

router.get('/home/main', async (ctx) => {
    let mainData =Mock.mock({
        'list|20':[{
            'id':'@increment',
            'title':'@ctitle(15,20)',
            'author':'@ctitle(5,8)',
            'time':'@ctitle(3,4)',
            'text':'@ctitle(20,50)',
            'label':'@ctitle(2,5)',
            'pic':Random.image('94x107'),
        }]
    })
    ctx.response.body = {
        success: true,
        data: mainData
    }
})

router.get('/home/list',async(ctx)=>{
    //参数 limit  page 

     let {limit = 20,page = 1}=ctx.request.query
    //to be continue 根据 limit 和page做数据筛选
        
    let data =Mock.mock({
        
    })
    ctx.body={
        success:true,
        data:data
    }
})

app
    .use(router.routes())
    .use(router.allowedMethods())


app.listen(9090, () => {
    console.log('server is running in port 9090')
})